package com.rede.didiok.pay.service.impl.PayServiceImpl;

import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.enums.CreditTypeEnum;
import com.rede.didiok.common.enums.OrderStatusEnum;
import com.rede.didiok.common.enums.PayMethodEnum;
import com.rede.didiok.common.enums.ResourceTypeEnum;
import com.rede.didiok.common.exception.exceptionType.InsertException;
import com.rede.didiok.common.exception.exceptionType.QueryException;
import com.rede.didiok.common.feign.WebFeignClient;
import com.rede.didiok.common.schema.CreditsChangeRequest;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.pay.annotation.PayMethod;
import com.rede.didiok.pay.entity.PayOrderEntity;
import com.rede.didiok.pay.service.PayOrderService;
import com.rede.didiok.pay.service.PayService;


/**
 * 积分支付服务
 *
 * @author 陌溪
 */
@Service
@PayMethod({PayMethodEnum.CREDITS_PAY})
@Slf4j
public class CreditsPayService implements PayService {
    @Resource
    private PayOrderService payOrderService;

    @Resource
    private WebFeignClient webFeignClient;

    @Override
    public String pay(String payOrderUid) {
        // 获取支付订单
        PayOrderEntity payOrder = payOrderService.getById(payOrderUid);
        if (payOrder == null) {
            log.error("支付订单不存在: payOrderUid: {}", payOrderUid);
            throw new InsertException("支付订单不存在");
        }
        // 获取需要支付的积分
        int price = payOrder.getTotalPrice().intValue();
        CreditsChangeRequest creditsChangeRequest = new CreditsChangeRequest();
        // 将资源类型变成积分变更类型
        ResourceTypeEnum resourceType = ResourceTypeEnum.getType(payOrder.getResourceType());
        CreditTypeEnum creditType = CreditTypeEnum.getCreditsByResourceType(resourceType);
        creditsChangeRequest.setCreditType(creditType);
        // 下单，肯定是扣除积分的
        creditsChangeRequest.setChangeCredits(-price);
        creditsChangeRequest.setResourceUid(payOrder.getResourceUid());
        creditsChangeRequest.setResourceType(resourceType);
        String result = webFeignClient.creditsChange(creditsChangeRequest);
        // 检查是否支付成功
        Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
        if (SysConf.SUCCESS.equals(resultMap.get(SysConf.CODE))) {
            log.info("支付成功");
            // 更新订单状态
            payOrder.setOrderStatus(OrderStatusEnum.Finish);
            payOrder.updateById();
            return ResultUtil.successWithMessage("支付成功");
        } else {
            if (resultMap.get(SysConf.MESSAGE) != null) {
                throw new QueryException(resultMap.get(SysConf.MESSAGE).toString());
            }
            throw new QueryException("支付异常，请稍后再试");
        }
    }

    @Override
    public String callback() {
        return null;
    }

    @Override
    public String closePayOrder(PayOrderEntity payOrder) {
        return null;
    }
}
